1. Field
The invention disclosed and claimed herein generally relates to a method for reducing the time that successive threads spend waiting to access a shared resource, wherein the threads are respectively provided by different processors. More particularly, the invention pertains to a method of the above type wherein a locking mechanism such as a handoff lock enables a thread to opportunistically gain access to the shared resource, ahead of threads which were previously waiting for access.
2. Description of the Related Art
As is well known by those of skill in the art, threads produced by different processors in a processor configuration may each seek to access a shared resource at the same time. The shared resources could be a specified database, a data object or a hardware device, by way of example. However, the shared resource may allow access to only one thread at any given time. Accordingly, locking mechanisms such as handoff locks have been developed, in order to provide an orderly procedure for scheduling access time to a shared resource for two or more threads, which all want to use the resource at the same time.
In its operation, a handoff locking mechanism initially provides a lock to a thread, from a particular processor, whereby such thread is granted access to a specified shared resource. While this thread holds the lock, other software threads that attempt to obtain the lock, in order to access the specified resource, will fail to do so. Accordingly, each of these threads will append itself to a list of lock waiters, i.e., threads waiting for the lock, wherein the list is associated with the locking mechanism.
Each of the waiter threads is thus effectively placed into a queue, and is also placed into a sleep state. When the thread holding the lock releases the lock, it will select one of the waiter threads to be woken up (or the sole waiter if there is only one), and will then “hand off” the lock to the selected thread. The selected thread will be the one that has been waiting longest in the queue, or that has highest priority based on some other prespecified criterion. The handoff lock mechanism thus provides fairness and orderliness in making the lock available to threads from different processors, to enable each of them to access the shared resource in turn.
In the use of a handoff lock as described above, each thread that holds the lock will typically take a time A to perform a specified task, while it has access to the shared resource. In addition, following release of the lock by the thread holding it, an additional time B can be required, in order to select the waiter thread with first priority, to then wake up the selected thread, and to finally hand off the lock to the selected thread. Thus, a total time of A+B could be required for each waiting thread. If the number of waiter threads is large, for example 10, an additional thread sent from a processor to use the shared resource may have to wait on the order of 10 (A+B) for access to the shared resource. Moreover, it is quite common for B to be significantly greater than A. In this situation, if the number of waiting threads is substantial, the amount of time that a shared resource is not performing useful work will be comparatively large, even though a number of threads are waiting to access the resource.